package com.goods.controller.business;

import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "物资来源")
@RestController
@RequestMapping("/business/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;
    //查询
/*    @GetMapping("/findSupplierList")
    public ResponseBean findSupplierList(Integer pageNum,Integer pageSize,String name){
        List<SupplierVO> list = supplierService.findSupplierList(name);
        List<SupplierVO> page = ListPageUtils.page(list, pageSize, pageNum);
        Map hashMap = new HashMap();
        hashMap.put("rows",page);
        hashMap.put("total",list.size());
        return ResponseBean.success(hashMap);
    }*/


    //查询
    @GetMapping("/findSupplierList")
    public ResponseBean findSupplierList(Integer pageNum,Integer pageSize, Supplier supplier){
        List<SupplierVO> list = supplierService.getSupplierList(supplier);
        List<SupplierVO> page = ListPageUtils.page(list, pageSize, pageNum);
        return ResponseBean.success(new PageVO<>(list.size(),page));
    }
    //添加
    @PostMapping("/add")
    public ResponseBean addSupplier(@RequestBody Supplier supplier){
        Integer integer = supplierService.addSupplier(supplier);
        if (integer > 0){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("添加失败惹");
        }
    }
    //回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        SupplierVO supplier = supplierService.edit(id);
        return ResponseBean.success(supplier);
    }
    //修改
    @PutMapping("update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody Supplier supplier){
      Integer integer = supplierService.update(id,supplier);
        if (integer > 0){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("修改失败惹");
        }
    }
    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean update(@PathVariable Long id){
        Integer integer = supplierService.delete(id);
        if (integer > 0){
            return ResponseBean.success();
        }else {
            return ResponseBean.error("删除失败惹");
        }
    }
    //查询所有
    @GetMapping("/findAll")
    private ResponseBean findAll(){
        List<SupplierVO> list = supplierService.findAll();
        return ResponseBean.success(list);
    }
}
